import bs4
import json
import requests
import pandas as pd


#常规模板
def getContent(url):
    try:
        agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
        response=requests.post(url=url,headers={'User-Agent':agent})
        response.raise_for_status()  #如果状态码不是200则抛出异常
        response.encoding=response.apparent_encoding   #判断网页的编码格式
    except Exception as e:
        print("爬取错误")
    else:
        print("爬取成功")
        print(response.content.decode('utf-8'))
        return response.content.decode('utf-8')


def parserContent(htmlContent):
    print("解析内容")


if __name__ == '__main__':
    url="http://www.xinfadi.com.cn/getPriceData.html"
    htmlContent=getContent(url)
    #从json中提取数据
    sava=json.loads(htmlContent)
    print(sava.keys())
    date=sava["list"][0]["prodName"]
    ov=pd.DataFrame(date)   #Warning Codeding error  Required pandas Support
    #parserContent(htmlContent)
    print("over!!")
    print(ov)

